package com.meichis.yndms.ui.activity;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.gson.Gson;
import com.meichis.mcsappframework.dialog.DropDownDialog;
import com.meichis.mcsappframework.utils.ScreenUtil;
import com.meichis.yndms.R;
import com.meichis.yndms.adapter.VstAnalyseAdapter;
import com.meichis.yndms.databinding.ActivityVstAnalyseBinding;
import com.meichis.yndms.hybrid.*;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.VstAnalyseVM;

import java.util.ArrayList;
import java.util.Collections;

public class VstAnalyseActivity extends BaseActivity implements HybridInterface {

    private ActivityVstAnalyseBinding binding;
    private VstAnalyseVM vm;
    private VstAnalyseAdapter adapter;


    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_vst_analyse);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(VstAnalyseVM.class);
        binding.setVm(vm);
        binding.setClick(new ClickEvent());
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void init() {
        binding.navigation.txtTitle.setText("拜访达成");
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());
        addObserver();

        adapter = new VstAnalyseAdapter(this, R.layout.item_vst_analyse, new ArrayList<>());
        binding.rc.setLayoutManager(new LinearLayoutManager(this));
        binding.rc.setAdapter(adapter);

        vm.GetYWBFFX();
    }

    private void addObserver() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));

        vm.getVstAnalyseList().observe(this, vstAnalyses -> {
            if (vstAnalyses.size() == 0) {
                binding.llTitle.setVisibility(View.GONE);
                binding.llWeb.setVisibility(View.GONE);
            }else {
                binding.llTitle.setVisibility(View.VISIBLE);
                binding.llWeb.setVisibility(View.VISIBLE);
            }
            adapter.setList(vstAnalyses);

            //倒叙
            Collections.reverse(vstAnalyses);

            ViewGroup.LayoutParams lp;
            lp = binding.llWeb.getLayoutParams();
            lp.width = binding.llWeb.getWidth();
            lp.height = ScreenUtil.dip2px(200) + ScreenUtil.dip2px(52) * vstAnalyses.size();
            binding.llWeb.setLayoutParams(lp);
            //统计图
            MCWebView web = new MCWebView(getActivity());
            MCSWebViewClient webViewClient = new MCSWebViewClient();
            MCSChromeClient webChromeClient = new MCSChromeClient(this);
            web.setWebViewClient(webViewClient);
            web.setWebChromeClient(webChromeClient);
            web.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1.0F));
            web.setBackgroundColor(Color.parseColor("#ffffff"));
            //加载本地资源文件
            web.loadUrl("file:///android_asset/VstAnalyse.html");
            binding.llWeb.removeAllViews();
            binding.llWeb.addView(web);
            web.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    web.sendResult("", "JavaCallJs",new Gson().toJson(vstAnalyses));
                }
            });
        });
    }

    @Override
    public void execute(int CallType, String ControlID, Object Revalue, String FUNCCallBack) {

    }

    @Override
    public void setActivityResultCallback(CallbackContext callback) {

    }


    public class ClickEvent {

        public void onSelectDateType() {
            new DropDownDialog<>(VstAnalyseActivity.this, R.color.theme, "时间维度", "Name", vm.getDataTableList(), new DropDownDialog.SelectedListener() {
                @Override
                public void setSelect(Object o, int position) {
                    vm.getDataTable().setValue(vm.getDataTableList().get(position));
                    vm.GetYWBFFX();
                }
            }).show();
        }
    }
}
